# -*- coding: utf-8 -*- # 
import os
import sys
import wx.grid
import data as dataobj

bookshelfs = [u"书架1",u"小説とか",u"书架2",[u"书架3",[u"分层1",u"分层2"]]]
ROOTNAME = u"我的书架"

#shelfPopMenu = [["添加子项目","删除该项目","更名"],["取消",]]
shelfPopMenu = [["添加子项目","onAddSubShelf"],
                ["删除该项目","onDelShelf"],
                ["删除所有子项目","onDelAllSub"],
                ["更名","onChgShelfName"]]
errmsg = {
        "00001":[u"项目重名",u"请重新指定名字"]
        }
addSubShelfDlg = ["请输入子项目名","输入子项目",""]
bottonsStr = {"00001":       u"搜索",
              "00002":       u"确定"}
bookTableTitles = [
            (u'',     0,     'right'),
            (u'编号',     70,     'right'),
            (u'书名',     300,     'left'),
            (u'作者',     120,    'left'),
            (u'译者',     120,    'left'),
            (u'备注',     250,    'left'),
            ]
searchFrameTitle = u"书籍检索"
SearchRltTableTitles = [
            (u'',     0,     'right'),
            (u'编号',     70,     'right'),
            (u'书名',     300,     'left'),
            (u'作者',     120,    'left'),
            (u'出版时期',     120,    'left'),
            ]
def get(attr):
    return getattr(dataobj, attr)
def getRootName():
    return passUTF8(ROOTNAME)
def getBookSelfs():
    #return passListUTF8(bookshelfs)
    return bookshelfs
def passUTF8(str):
    return str.decode("UTF8")
    #return unicode(str)
def passListUTF8(list):
    newList = []
    for item in list:
        if type(item) == str:
            newList.append(passUTF8(item))
        else:
            tmpList = passListUTF8(item)
            newList.append(tmpList)
    return newList
def getShelfPopMenu():
    return passListUTF8(shelfPopMenu)
def getAddSubShelfDlg():
    return passListUTF8(addSubShelfDlg)
def showErrMsgDig(msgid):
        #msgList = passListUTF8(errmsg[msgid])
        msgList = errmsg[msgid]
        retCode = wx.MessageBox(msgList[1],
            msgList[0], wx.OK | wx.ICON_ERROR)
        #dlg.Destroy()
def getButtonsStr(id):
    caption = passUTF8(bottonsStr[id])
    return caption
    
    
class BookListGrid(wx.grid.Grid):
    listdata = (("CF", "Bob", "Dernier"), ("2B", "Ryne", "Sandberg"),
        ("LF", "Gary", "Matthews"), ("1B", "Leon", "Durham"),
        ("RF", "Keith", "Moreland"), ("3B", "Ron", "Cey"),
        ("C", "Jody", "Davis"), ("SS", "Larry", "Bowa"),
        ("P", "Rick", "Sutcliffe"))
    def __init__(self, parent):
        wx.grid.Grid.__init__(self, parent, -1)
        entrys = []
        for i in self.listdata:
            entrys.append(LineupEntry(i[0],i[1],i[2]))
        self.SetTable(LineupTable(entrys)) 

class LineupEntry:

    def __init__(self, pos, first, last):
        self.pos = pos
        self.first = first
        self.last = last

class LineupTable(wx.grid.PyGridTableBase):

    colLabels = ("First", "Last") # 列  
    colAttrs = ("first", "last") #1 属性名

    def __init__(self, entries): #2 初始化模型
        wx.grid.PyGridTableBase.__init__(self)
        self.entries = entries

    def GetNumberRows(self):
        return len(self.entries)

    def GetNumberCols(self):
        return 2

    def GetColLabelValue(self, col):
        return self.colLabels[col] # 列  

    def GetRowLabelValue(self, col):
        return self.entries[col].pos #3  行  

    def IsEmptyCell(self, row, col):
        return False

    def GetValue(self, row, col):
        entry = self.entries[row]
        return getattr(entry, self.colAttrs[col]) #4  属性 

    def SetValue(self, row, col, value):
        pass